package easier.app.x.view;

import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import easier.app.x.R;

/**
 * 在应用界面弹出窗口
 */
public abstract class ActivityMessage {

    /**
     * @param activity 显示在当前界面最上边
     */
    public void show( Activity activity ) {

        View decorView = activity.getWindow().getDecorView();

        PopupWindow popupWindow;
        View view = onCreateView( decorView );
        popupWindow = new PopupWindow( view );
        popupWindow.setWidth( decorView.getWidth() );
        popupWindow.setHeight( LayoutParams.WRAP_CONTENT );

        popupWindow.setFocusable( true );
        popupWindow.setOutsideTouchable( false );

        popupWindow.setAnimationStyle( R.style.top_notify );
        popupWindow.setClippingEnabled( false );

        view.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                onMessageClick( v );
                popupWindow.dismiss();
            }
        } );

        /* 5s后消失 */
        view.postDelayed( new Runnable() {
            @Override
            public void run() {

                popupWindow.dismiss();
            }
        }, 5000 );

        /* 设置回调 */
        popupWindow.setOnDismissListener( new OnDismissListener() {
            @Override
            public void onDismiss() {

                ActivityMessage.this.onDismissCallback();
            }
        } );

        popupWindow.showAtLocation( decorView, Gravity.TOP, 0, 100 );
    }

    /**
     * 实现创建一个通知界面
     *
     * @param parent 父布局
     *
     * @return 通知界面
     */
    protected abstract View onCreateView( View parent );

    /**
     * 消失的回调
     */
    protected void onDismissCallback() {

    }

    /**
     * @param view 消息界面点击的回调
     */
    protected void onMessageClick( View view ) {

    }
}
